<?php

/**
 * Created by Generate
 * User: Xiaodao
 * Coding Standard: PSR2
 * Date: 2018-12-06
 * Time: 15:17
 */

namespace app\htsoft\controller;
use think\Db;
use think\Url;
use app\common\controller\Base;
class Classify extends Base
{
    private $whereList = ['id' => 'eq','name' => 'like '];
    function _initialize() {
        parent::_initialize();
    }
    function classifylist(){
        if(IS_POST){
            $map=[];
            $nowpage = input('page',1, 'intval');
            $limits = input('limit',10, 'intval');
            parse_str(input('para'), $wherearray);
            $whereList = $this->whereList;
            foreach ($wherearray as $k => $v) {
                if (isset($v) && (strlen($v) > 0)) {

                    if (key_exists($k, $whereList)) {
                        if (preg_match("/_id$/i", $k)) {
                            $map["a.".$k]=['eq',$v];
                            continue;
                        }
                        if ('eq' === $whereList[$k]) {$map[$k]= ['eq',$v];} else {$map[$k]= ['like',"'%{$v}%'"] ;}
                    }
                }
            }
            $data=Db::name('classify')
                ->where($map)-> page($nowpage, $limits)
                -> select();
            $count=Db::name('classify')
                ->where($map)-> count();
            echo json_encode(['status'=>200,'data'=>$data,'msg'=>'ok','total'=>$count]);
        }else{
            
            return $this->fetch();
        }
    }
    function classifyadd(){
        if(IS_POST){
            $posts=input();
            $posts['created_at']=time();
            $posts['updated_at']=time();
            foreach ($posts as $k => $v) {
                if (is_array($v)) {
                    $posts[$k] = implode($v, ',');
                }
            }
            $result = model("classify") -> save($posts);
            if ($result) {
                adminLog2("添加分类!");
                $this -> success("添加成功");
            } else {
                $this->error("服务器繁忙,请稍后重试!!");
            }
        }else{
            return $this -> fetch();
        }
    }
    function classifyedit(){
        if(IS_POST){
            $posts=input();
            $posts['updated_at']=time();
            foreach ($posts as $k => $v) {
                if (is_array($v)) {
                    $posts[$k] = implode($v, ',');
                }
            }
            $rst = model("classify") -> save($posts,["id"=>$posts["id"]]);
            if($rst){
                adminLog2("编辑修改分类!");
                $this -> success("编辑修改成功！");
            }else{
                $this -> error("网络繁忙，请重试！");
            }
        }else{
            $pk=input();
            $info=Db::name('classify')->find($pk);
            
            $this->assign('info',$info);
            return $this -> fetch();
        }
    }
    function classifyinfo(){
        $pk=input();
        $info=Db::name('classify')->find($pk);
        $this->assign('info',$info);
        return $this -> fetch();
    }
    function classifydelete(){
        $posts=input();
        $idArr=json_decode($posts['id'],true);
        $res=Db::name('classify')->where(['id'=>['in',$idArr]])->delete();
        if($res){
            adminLog2("删除分类!");
            $this -> success("删除成功");
        }else{
            $this->error("删除失败,请稍后重试!!");
        }
    }
}